package com.traffic.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollUtil;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.traffic.annotation.ApiJsonObject;
import com.traffic.annotation.ApiJsonProperty;
import com.traffic.constant.Status;
import com.traffic.entity.Issue;

import com.traffic.exception.BaseException;
import com.traffic.model.ApiResponse;

import com.traffic.service.IssueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@Api(tags = "常见问题相关接口")
@RestController
@RequestMapping(value = "/issues")
@Slf4j

//https://blog.csdn.net/liuyuanjiang109/article/details/78972644

public class IssueController {

    @Autowired
    IssueService issueService;

    @ApiOperation(value = "【常见问题】", notes = "关键词模糊查询+日期查询")
    //@ApiImplicitParam(paramType = "path", dataType = "String", name = "question", value = "查询问题", required = true)
    @PostMapping("/search")
    @ApiJsonObject(name = "param",value = {
            @ApiJsonProperty(type =String.class,key = "question",example = "问题2" , description = "问题标题"),
            @ApiJsonProperty(type =String.class,key = "startdate",example = "2020-10-10" , description = "开始日期"),
            @ApiJsonProperty(type =String.class,key = "enddate",example = "2020-10-21" , description = "结束日期"),

    })
    public ApiResponse searchQuestion(@RequestBody Map<String, String> param) {


        List<Issue> questions =  issueService.queryTargetIssue(param.get("question"),param.get("startdate"),param.get("enddate"));
        if (questions.isEmpty()) {
            return ApiResponse.ofStatus(Status.OK);
        }else {
            return ApiResponse.ofStatus(Status.OK, questions);
        }
    }

    @ApiOperation(value = "【常见问题】", notes = "全部问题")
    @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "page", value = "页面", required = true)
    @GetMapping("/all/{page}")
    public ApiResponse AllQuestion(@PathVariable Integer page) {

        Integer count = issueService.count();

        IPage<Issue> questionIPage = new Page<Issue>(page, 10,count);
        IPage<Issue> questions = issueService.page(questionIPage);

        return ApiResponse.ofStatus(Status.OK, questions);

    }

    @ApiOperation(value = "【常见问题】", notes = "新增问题")
    @PostMapping("/insert")
    @ApiJsonObject(name = "issue",value = {
            @ApiJsonProperty(type =String.class,key = "question",example = "问题2" , description = "问题标题"),
            @ApiJsonProperty(type =String.class,key = "answer",example = "解决方式为xxxxx" , description = "问题解决方式"),
    })
    public ApiResponse insertQuestion(@RequestBody Map<String, Object> issue) {

        String param[] = new String[]{"question","answer"};

        List<String> paramList = CollUtil.newArrayList(param);
        if (!paramList.containsAll(CollUtil.newArrayList(issue.keySet().toArray()))) {
            throw new BaseException(Status.PARAM_ERROR);
        }

        Issue iss = new Issue();
        BeanUtil.copyProperties(issue, iss, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        iss.setState(1);
        iss.setCreateTime(new DateTime());
        issueService.saveIssue(iss);

        return ApiResponse.ofStatus(Status.OK);
    }


    @ApiOperation(value = "【常见问题】", notes = "新增问题")
    @PostMapping("/update")
    @ApiJsonObject(name = "issue",value = {
            @ApiJsonProperty(type =String.class,key = "question",example = "问题2" , description = "问题标题"),
            @ApiJsonProperty(type =String.class,key = "answer",example = "解决方式为xxxxx" , description = "问题解决方式"),
    })
    public ApiResponse updateQuestion(@RequestBody Map<String, Object> issue) {

        String param[] = new String[]{"id","question","answer",};

        List<String> paramList = CollUtil.newArrayList(param);
        if (!paramList.containsAll(CollUtil.newArrayList(issue.keySet().toArray()))) {
            throw new BaseException(Status.PARAM_ERROR);
        }

        Issue iss = new Issue();
        BeanUtil.copyProperties(issue, iss, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        iss.setState(1);
        issueService.updateById(iss);

        return ApiResponse.ofStatus(Status.OK);
    }


    @ApiOperation(value = "【常见问题】", notes = "删除问题")
    @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "deleteId", value = "删除问题的id", required = true)
    @DeleteMapping(value = "/delete/{deleteId}")
    public ApiResponse deleteQuestion(@PathVariable Long deleteId) {

        issueService.deleteIssue(deleteId);

        return ApiResponse.ofStatus(Status.OK);
    }

    @ApiOperation(value = "【常见问题】", notes = "查看问题")
    @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "issId", value = "查看问题的id", required = true)
    @GetMapping("/look/{issId}")
    public ApiResponse LookQuestion(@PathVariable Integer issId) {

        Issue iss = issueService.getById(issId);

        return ApiResponse.ofStatus(Status.OK,iss);
    }





}
